package com.tsingteng.cosfun.ui.mine;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.AttentFansbean;
import com.tsingteng.cosfun.bean.AttentRecommendbean;
import com.tsingteng.cosfun.bean.FollowBean;
import com.tsingteng.cosfun.bean.RecommenResp;
import com.tsingteng.cosfun.callback.AppListener;
import com.tsingteng.cosfun.common.BroadcastManager;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.ui.mine.attent.AttentAdapter;
import com.tsingteng.cosfun.ui.mine.attent.AttentContract;
import com.tsingteng.cosfun.ui.mine.attent.AttentPresenter;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TopActionBar;


import de.hdodenhof.circleimageview.CircleImageView;


/**
 * author :zhikuan
 * Date:18-8-20
 */

public class AttentListFragment extends BasePresenterFragment<AttentPresenter, AttentContract.IAttentView> implements AttentContract.IAttentView {

    public static String FLAG = "flag";
    public static String ID = "user_id";
    private MyRecycleView rvFanRv;
    private String userId;
    private int page = 1;
    public int flag;
    private TextView emptyView, recommend;
    private TextView fans_empty, fans_empty_button;
    private MyRecycleView recycleView;
    private BaseQuickAdapter<AttentRecommendbean, BaseViewHolder> recommendAdapter;

    @Override
    protected void refresh() {
        page = 1;
        mAdapter.getData().clear();
        if (flag == 1) {
            mPresenter.getFansList(userId, page);
        } else {
            mPresenter.getAttentList(userId, page);

        }

    }

    @Override
    protected void initView() {
        fans_empty_button = rootView.findViewById(R.id.fans_empty_button);
        fans_empty = rootView.findViewById(R.id.fans_empty);
        fans_empty_button.setOnClickListener(v -> VideoHelper.getIntence().starVideoShoot(getActivity()));
        rvFanRv = rootView.findViewById(R.id.common_list);
        emptyView = rootView.findViewById(R.id.text_empty);
        recommend = rootView.findViewById(R.id.text_empty_recommend);
        recycleView = rootView.findViewById(R.id.recommend_list);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        recycleView.setLayoutManager(layoutManager);

        recommendAdapter = new BaseQuickAdapter<AttentRecommendbean, BaseViewHolder>(R.layout.attention_item2, null) {
            @Override
            protected void convert(BaseViewHolder helper, AttentRecommendbean item) {
                TextView attent = helper.getView(R.id.attention_item_attention);
                TextView name = helper.getView(R.id.attention_item_name);
                CircleImageView head = helper.getView(R.id.attention_item_img);
                switch (item.isAttention) {
                    case 0:
                        attent.setText("关注");
                        break;
                    case 1:
                        attent.setText("已关注");
                        break;
                    case 2:
                        attent.setText("互相关注");
                        break;
                    case -1:
                        attent.setText("");
                        break;
                }
//                if (!TextUtils.isEmpty(item.profileAvatar)) { //头像
                ImageUtils.LoadImage(mContext, item.profileAvatar+ Config.YA_SUO_IMAGE_URL, head);
//                }
                if (!TextUtils.isEmpty(item.profileName)) {
                    name.setText(item.profileName);
                }
                head.setOnClickListener(view -> {
                    Bundle bundle = new Bundle();
                    bundle.putInt("user_id", item.profileId);
                    bundle.putInt(Constant.TAG, 1);
                    TerminalActivity.showFragment(getContext(), HomeMineFragment.class, bundle);
                });
                attent.setOnClickListener(view -> {
                    if ("关注".contentEquals(attent.getText())) {
                        onFocus(item.profileId+"::1");
                        mPresenter.loginFollowFans(item.profileId + "", helper.getAdapterPosition());
                    } else {
                        onFocus(item.profileId+"::2");
                        mPresenter.loginNoFollow(item.profileId + "", helper.getAdapterPosition());
                    }
                });
            }
            void onFocus(String focus){
                AppListener.OnFocusBackListener backListener = AppListener.getInstance().getFocusBackListener();
                if (null !=backListener){
                    backListener.onFocusBack(focus);
                }
            }

        };

        recycleView.setAdapter(recommendAdapter);
        recycleView.setMyRefreshListener(() -> mPresenter.getReList());

        super.initView();
    }


    @Override
    protected void initData(View rootView) {
        Bundle bundle = getArguments();
        if (null == bundle){
            return;
        }
        if (bundle.containsKey(FLAG)) {
            flag = bundle.getInt(FLAG);
        }
        if (bundle.containsKey(ID)) {
            userId = bundle.getInt(ID) + "";
        }

        TopActionBar topActionBar = rootView.findViewById(R.id.topbar);
        if (flag == 1) {
            if (TextUtils.equals(userId, String.valueOf(LogonUtils.getProFildId()))) {
                topActionBar.setTitle("我的粉丝");
            } else {
                topActionBar.setTitle("TA的粉丝");
            }
            mPresenter.getFansList(userId, page);
        } else {
            if (TextUtils.equals(userId, String.valueOf(LogonUtils.getProFildId()))) {
                topActionBar.setTitle("我的关注");
            } else {
                topActionBar.setTitle("TA的关注");
            }
            mPresenter.getAttentList(userId, page);
        }

    }

    @Override
    protected AttentPresenter createPresenter() {
        return new AttentPresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return rvFanRv;
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return new AttentAdapter(getPresenter());
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        if (flag == 1) {
            mPresenter.getFansList(userId, page);

        } else {
            mPresenter.getAttentList(userId, page);

        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_common_list;
    }

    @Override
    public void getFollowResult(FollowBean followBean, int position) {
        Toast.makeText(getContext(), "关注成功", Toast.LENGTH_LONG).show();
        if (recycleView.getVisibility() == View.VISIBLE) {
            mPresenter.getReList();
            recommendAdapter.getItem(position).isAttention = followBean.getRelationType();
            recommendAdapter.notifyDataSetChanged();
        } else {

            AttentFansbean.Data data = (AttentFansbean.Data) mAdapter.getItem(position);
            if (null == data){
                return;
            }
            data.followType = followBean.getRelationType();
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getNoFollowResult(FollowBean followBean, int position) {
        Toast.makeText(getContext(), "取消关注成功", Toast.LENGTH_LONG).show();
        if (recycleView.getVisibility() == View.VISIBLE) {
            mPresenter.getReList();
            recommendAdapter.getItem(position).isAttention = followBean.getRelationType();
            recommendAdapter.notifyDataSetChanged();
        } else {

            AttentFansbean.Data data = (AttentFansbean.Data) mAdapter.getItem(position);
            if (null == data){
                return;
            }
            data.followType = followBean.getRelationType();
            mAdapter.notifyDataSetChanged();
        }
        AppListener.OnNoFollowBackListener listener = AppListener.getInstance().getNoFollowBackListener();
        if (null != listener){
            listener.onNoFollowBack();
        }
    }

    @Override
    public void getShowListData(AttentFansbean attentFansbean) {
        rvFanRv.setEnableRefresh(true);
        if (rvFanRv != null) rvFanRv.refreshComplete();
        if (attentFansbean == null) {
            mAdapter.loadMoreComplete();
        } else {
            mAdapter.loadMoreEnd();
        }
        if (null == attentFansbean){
            return;
        }
        if (attentFansbean.data != null) {
            mAdapter.addData(attentFansbean.data);
            mAdapter.notifyDataSetChanged();
        }
        if (mAdapter.getItemCount() == 0) {
            rvFanRv.setEnableRefresh(false);
            if (flag == 1) {
                fans_empty_button.setVisibility(View.VISIBLE);
                fans_empty.setVisibility(View.VISIBLE);

            } else {
                recommend.setVisibility(View.VISIBLE);
                emptyView.setVisibility(View.VISIBLE);
                recycleView.setVisibility(View.VISIBLE);
                if (Integer.parseInt(userId) == LogonUtils.getProFildId()) {
                    mPresenter.getReList();
                }

            }

        } else {
            if (flag == 1) {
                fans_empty_button.setVisibility(View.GONE);
                fans_empty.setVisibility(View.GONE);

            } else {
                recommend.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);
                recycleView.setVisibility(View.GONE);

            }

        }
    }

    @Override
    public void getShowListData(RecommenResp attentRecommendbean) {
        recycleView.refreshComplete();
        if (attentRecommendbean != null) {
            if (recommendAdapter.getData() != null)
                recommendAdapter.getData().clear();
            recommendAdapter.addData(attentRecommendbean.getData());
        }
    }


    public AttentPresenter getPresenter() {
        return mPresenter;
    }

}
